package A一周刷爆LeetCode.A基础班.G图;

import java.util.HashSet;
import java.util.LinkedList;

/**
 * @author wei.zhao
 * @description: 宽度优先遍历
 * @date 2022年03月04日 13:56
 */
public class Code01_BFS {

    public static void bfs(Node head) {
        if (head == null) {
            return;
        }
        LinkedList<Node> list = new LinkedList<>();
        HashSet<Node> set = new HashSet<>();
        list.add(head);
        set.add(head);
        while (!list.isEmpty()) {
            Node node = list.pop();
            System.out.println(node.value);
            for (Node next : node.nexts) {
                if (!set.contains(next)) {
                    set.add(next);
                    list.add(next);
                }
            }
        }
    }

}
